#!/usr/bin/env python
# -*- coding: utf-8 -*-
import threading
from flask_sqlalchemy import SQLAlchemy

Lock = threading.Lock()


class PgSqlManager(object):
    """
    功能：单件模式客户端。
    """

    __instance = None
    __db = None

    def __init__(self):
        pass

    def __new__(cls, *args, **kwargs):
        if (not cls.__instance or not cls.__db):
            try:
                Lock.acquire()
                # double check
                if not cls.__instance:
                    cls.__instance = super(PgSqlManager, cls).__new__(cls, *args, **kwargs)
                    cls.__db = SQLAlchemy()
            finally:
                Lock.release()
        return cls.__instance

    def get_instance(self):
        return self.__db

    def set_instance(self, obj):
        print(obj)
        self.__db = obj